package com.xxx.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "统一响应结果对象")
@Builder
//@JsonIgnore(JsonInclude.Include.NON_NULL)
public class Result<T> {
    @Schema(description = "状态码")
    private Integer code;
    @Schema(description = "响应信息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;
    public static <E> Result<E> success(E data){
        return new Result<>(Code.OK,"操作成功",data);
    }

    public static <E> Result<E> success(String message,E data){
        return new Result<>(Code.OK,message,data);
    }
    public static Result success(){
        return new Result(Code.OK,"操作成功",null);
    }

    public static  Result error(){
        return new Result(Code.ERROR,"fail",null);
    }
    public static  Result error(String message){
        return new Result(Code.ERROR,message,null);
    }

    public static  Result error(Integer code, String message){
        return new Result(code, message,null);
    }

    public static Result check(Boolean ok){
        if(ok)return Result.success();
        else return Result.error();
    }
    public static <E> Result<E> check(Boolean ok, E data){
        if(ok)return Result.success(data);
        else return Result.error();
    }
}
